package com.example.tian.securityinspection.controller;

import com.example.tian.securityinspection.base.BaseActivity;
import com.example.tian.securityinspection.base.BaseController;
import com.example.tian.securityinspection.model.bean.ResponseError;
import com.example.tian.securityinspection.model.bean.TestBean;
import com.example.tian.securityinspection.network.RequestCallback;
import com.example.tian.securityinspection.network.RestApiClient;
import com.google.common.base.Preconditions;

import javax.inject.Inject;

import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class TestController extends BaseController<TestController.TestUi, TestController.TestUiCallbacks>{

    //网络请求器
    private final RestApiClient mRestApiClient;

    @Inject
    public TestController(RestApiClient restApiClient){
        super();
        mRestApiClient = Preconditions.checkNotNull(restApiClient, "restApiClient cannot be null");
    }

    @Override
    protected void onInited() {
        super.onInited();
    }

    @Override
    protected void onSuspended() {
        super.onSuspended();
    }

    @Override
    protected synchronized void populateUi(TestUi ui) {
        if (ui instanceof TestUi){
            //执行操作
            //doTestPost(ui);
        }
    }

    private void doTestPost(final TestUi ui) {

        //进行测试用的网络请求操作
        mRestApiClient.testService()
                .doTest("test")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new RequestCallback<TestBean>() {

                    //请求成功调用
                    @Override
                    public void onResponse(TestBean response) {
                        super.onResponse(response);
                        if (ui instanceof TestUi){
                            ui.onFinish();
                        }
                    }

                    //请求失败调用
                    @Override
                    public void onFailure(ResponseError error) {
                        super.onFailure(error);
                    }
                });

    }

    @Override
    protected TestUiCallbacks createUiCallbacks(TestUi ui) {
        return new TestUiCallbacks() {

            @Override
            public void doSomeThing() {

            }
        };
    }


    public interface TestUiCallbacks{

       void doSomeThing();

    }

    public interface TestUi extends BaseController.Ui<TestUiCallbacks>{

        void onFinish();
    }



}
